package cn.ac.van.imeigen.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ResourceUtils;

import java.io.*;

public class FileLoadUtil {
  public static File loadFile(String path, String fileName) throws IOException {
    ClassPathResource resource = new ClassPathResource(path + fileName);
    InputStream in = resource.getInputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);
    File targetFile = new File("/tmp/tempfile" + fileName);
//        File targetFile = new File("D:\\" + fileName);
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    return targetFile;
    //    return ResourceUtils.getFile("/var/www/html/res/fonts/" + fontName);
  }

  public static File loadFont(String fontName) throws Exception {
    return loadFile("\\static\\fonts\\", fontName);
  }

  public static File loadBackGround(String bgName) throws Exception {
    return loadFile("\\static\\backgrounds\\", bgName);
  }
}
